package com.manager.composite.controller;

import com.manager.common.constant.TcpDataType;
import com.manager.common.customer.utils.RemoteExecute;
import com.manager.common.utils.DataTools;
import com.manager.common.vo.ResultVO;
import com.manager.container.service.DockerDeployService;
import com.manager.model.DockerDeploy;
import com.manager.model.SensitiveWord;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/decision")
public class DecisionController {

    @Autowired
    DockerDeployService dockerDeployService;

    @RequestMapping("/handleDecisionSoft")
    public ResultVO handleDecisionSoft(String state) {
        try{
            String url = DataTools.getSystemSettingStringValByKey("issue_instructions_url");

            JSONObject params = new JSONObject();
            String serialNumber = DataTools.getSerialNumber();
            params.put("serialNumber", serialNumber);
            params.put("code",Integer.valueOf(state));

            JSONObject request = new JSONObject();
            request.put("code", TcpDataType.MIMICRY_HANDLE.getCode());
            request.put("content",params);

            ResultVO resultVO = RemoteExecute.executeTcpInstruction(url, request.toString());
            DataTools.putWebSessionId(serialNumber);

            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/judgment")
    public ResultVO judgment(String containerIds,String handleCode,int time) {
        try{
            List<DockerDeploy> list = dockerDeployService.getDockerDeployByIds(containerIds);
            String url = DataTools.getSystemSettingStringValByKey("issue_instructions_url");
            if(list == null || list.size() == 0){
                return ResultVO.failed("没有查询到可用容器!");
            }
            JSONObject params = new JSONObject();
            JSONArray decisionArray = new JSONArray();
            String serialNumber = DataTools.getSerialNumber();
            params.put("serialNumber", serialNumber);
            params.put("decisions",decisionArray);

            JSONObject handle = new JSONObject();
            handle.put("code",Integer.valueOf(handleCode));
            JSONArray contianerIds = new JSONArray();
            for(DockerDeploy dockerDeploy : list){
                contianerIds.add(dockerDeploy.getContainerID());
            }
            handle.put("containerIds",contianerIds);

            decisionArray.add(handle);
            params.put("decisions",decisionArray);
            params.put("time",time);

            JSONObject request = new JSONObject();
            request.put("code", TcpDataType.DECISION_HANDLE.getCode());
            request.put("content",params);

            ResultVO resultVO = RemoteExecute.executeTcpInstruction(url, request.toString());
            DataTools.putWebSessionId(serialNumber);

            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }
}
